﻿using MikuMikuModel.Logs;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ft_module_parser
{
    class a3ds
    {
        public List<a3d> a3d_dbs = new List<a3d>();
        public List<a3d> a3d_dbs_uid = new List<a3d>();

        List<a3d> ft_mdata = new List<a3d>();
        List<a3d> ft = new List<a3d>();
        List<a3d> ac = new List<a3d>();
        List<a3d> merged = new List<a3d>();

        List<a3d> ft_mdata_uid = new List<a3d>();
        List<a3d> ft_uid = new List<a3d>();
        List<a3d> ac_uid = new List<a3d>();
        List<a3d> merged_uid = new List<a3d>();
        
        public void load(string file)
        {
            StreamReader srdlc = new StreamReader(file);
            while (srdlc.Peek() > -1)
            {
                string line = srdlc.ReadLine();
                
                bool check = true;

                if (line.StartsWith("#"))
                    check = false;

                if (line.Contains("length"))
                    check = false;

                
                if (check)
                {
                    string[] line2 = line.Split('.');
                    int id = int.Parse(line2[1]);

                    if (line2[0] == "category")
                    {
                        var curRawPv = a3d_dbs.Where(c => c.id == id).FirstOrDefault();
                        if (curRawPv == null)
                        {
                            a3d_dbs.Add(new a3d(id));
                            curRawPv = a3d_dbs.Where(c => c.id == id).FirstOrDefault();
                            curRawPv.rawrows.Add(format_str(line2));
                        }
                        else
                        {
                            curRawPv.rawrows.Add(format_str(line2));
                        }

                        if (line2[2].Contains("value"))
                        {
                            var line3 = line2[2].Split('=');
                            curRawPv.value = line3[1];
                        }
                    }

                    if (line2[0] == "uid")
                    {
                        var curRawPv = a3d_dbs_uid.Where(c => c.id == id).FirstOrDefault();
                        if (curRawPv == null)
                        {
                            a3d_dbs_uid.Add(new a3d(id));
                            curRawPv = a3d_dbs_uid.Where(c => c.id == id).FirstOrDefault();
                            if (!line.Contains("org_uid"))
                            {
                                curRawPv.rawrows.Add(format_str(line2));
                            }
                        }
                        else
                        {
                            if (!line.Contains("org_uid"))
                                curRawPv.rawrows.Add(format_str(line2));
                        }

                        if (line2[2].Contains("value"))
                        {
                            var line3 = line2[2].Split('=');
                            curRawPv.value = line3[1];
                        }

                        if (line2[2].Contains("org_uid"))
                        {
                            var line3 = line2[2].Split('=');
                            curRawPv.org_id = int.Parse(line3[1]);
                        }
                    }
                }
            }
            srdlc.Close();
        }
        
        public void save(string s)
        {
            StreamWriter outp = new StreamWriter(s, false, Encoding.ASCII);
            outp.NewLine = "\n";
            outp.WriteLine("#A3DA__________");
            outp.WriteLine("# date time was eliminated.");

            var merged_sorted = a3d_dbs.OrderBy(o => o.value).ToList();
            merged_sorted = merged_sorted.OrderBy(o => o.id.ToString()).ToList();

            //category
            foreach (var i in merged_sorted)
            {
                var header = "category." + i.id;
                foreach (var u in i.rawrows)
                {
                    outp.WriteLine(header + u);
                }
            }
            outp.WriteLine("category.length=" + (merged_sorted.Max(c => c.id) + 1));
            //a3d
            merged_sorted = a3d_dbs_uid.OrderBy(o => o.value.ToString()).ToList();
            merged_sorted = merged_sorted.OrderBy(o => o.id.ToString()).ToList();

            foreach (var i in merged_sorted)
            {
                var header = "uid." + i.id;
                foreach (var u in i.rawrows)
                {
                    outp.WriteLine(header + u);
                }
            }

            outp.WriteLine("uid.length=" + (merged_sorted.Max(c => c.id) + 1));
            outp.Close();
        }

        string format_str(string[] line2)
        {
            string rawRow = "";
            int i = 2;
            while (i < line2.Length)
            {
                if (i == 2)
                    rawRow = "." + line2[i];
                else rawRow = rawRow + "." + line2[i];
                i++;
            }
            return rawRow;
        }

        public void FixMissing()
        {
            StreamReader csv = new StreamReader("auth3d_csv.txt");
            StreamReader srac = new StreamReader("auth_3d_output2.txt");
            StreamWriter outp = new StreamWriter("auth_3d_output_fixed.txt");

            while (srac.Peek() > -1)
            {
                string line = srac.ReadLine();
                string[] line2 = line.Split('.');

                int id = int.Parse(line2[1]);

                if (line2[0] == "category")
                {
                    var curRawPv = ac.Where(c => c.id == id).FirstOrDefault();
                    if (curRawPv == null)
                    {
                        ac.Add(new a3d(id));
                        curRawPv = ac.Where(c => c.id == id).FirstOrDefault();
                        curRawPv.rawrows.Add(format_str(line2));
                    }
                    else
                    {
                        curRawPv.rawrows.Add(format_str(line2));
                    }

                    if (line2[2].Contains("value"))
                    {
                        var line3 = line2[2].Split('=');
                        curRawPv.value = line3[1];
                    }
                }

                if (line2[0] == "uid")
                {
                    var curRawPv = ac_uid.Where(c => c.id == id).FirstOrDefault();
                    if (curRawPv == null)
                    {
                        ac_uid.Add(new a3d(id));
                        curRawPv = ac_uid.Where(c => c.id == id).FirstOrDefault();
                        curRawPv.rawrows.Add(format_str(line2));
                    }
                    else
                    {
                        curRawPv.rawrows.Add(format_str(line2));
                    }

                    if (line2[2].Contains("value"))
                    {
                        var line3 = line2[2].Split('=');
                        curRawPv.value = line3[1];
                    }
                }
            }

            int maxids = 0;
            while (csv.Peek() > -1)
            {
                string line = csv.ReadLine();
                string[] line2 = line.Split(',');
                a3d newa3d = new a3d(maxids);
                newa3d.value = "A " + line2[1];
                string rawrows2 = ".category=" + line2[0];
                string rawrows3 = ".size=" + line2[2];
                string rawrows1 = ".value=" + newa3d.value;
                ft.Add(newa3d);
                newa3d.rawrows.Add(rawrows2);
                newa3d.rawrows.Add(rawrows3);
                newa3d.rawrows.Add(rawrows1);
                maxids++;
            }

            foreach (var a in ac)
            {
                var check = merged.Where(c => c.value == a.value).FirstOrDefault();
                if (check == null)
                {
                    var check2 = merged.Where(c => c.id == a.id).FirstOrDefault();
                    if (check2 == null)
                    {
                        merged.Add(a);
                    }
                    else
                    {
                        int maxid = merged.Max(c => c.id) + 1;
                        a.id = maxid;
                        merged.Add(a);
                    }
                }
            }

            foreach (var a in ac_uid)
            {
                var check = merged_uid.Where(c => c.value == a.value).FirstOrDefault();
                if (check == null)
                {
                    var check2 = merged_uid.Where(c => c.id == a.id).FirstOrDefault();
                    if (check2 == null)
                    {
                        merged_uid.Add(a);
                    }
                    else
                    {
                        int maxid = merged_uid.Max(c => c.id) + 1;
                        a.id = maxid;
                        merged_uid.Add(a);
                    }
                }
            }

            foreach (var a in ft)
            {
                var check = merged_uid.Where(c => c.value == a.value).FirstOrDefault();
                if (check == null)
                {
                    var check2 = merged_uid.Where(c => c.id == a.id).FirstOrDefault();
                    if (check2 == null)
                    {
                        merged_uid.Add(a);
                        Console.WriteLine("WTF in " + a.value);
                    }
                    else
                    {
                        int maxid = merged_uid.Max(c => c.id) + 1;
                        Console.WriteLine("OK " + a.value + " " + a.id);
                        a.id = maxid;
                        merged_uid.Add(a);
                    }
                }
            }

            outp.WriteLine("#A3DA__________");
            outp.WriteLine("# date time was eliminated.");

            foreach (var i in merged)
            {
                if (i.id != 0)
                {
                    i.tempid = i.id;
                    while (i.tempid <= 1000000)
                        i.tempid = i.tempid * 10;
                }

            }

            var merged_sorted = merged.OrderBy(o => o.value).ToList();
            merged_sorted = merged_sorted.OrderBy(o => o.id.ToString()).ToList();

            foreach (var i in merged_sorted)
            {
                var header = "category." + i.id;
                foreach (var u in i.rawrows)
                {
                    outp.WriteLine(header + u);
                    //Console.WriteLine(header + u);
                }
            }
            outp.WriteLine("category.length=" + (merged.Max(c => c.id) + 1));
            //a3d
            merged_sorted = merged_uid.OrderBy(o => o.value.ToString()).ToList();
            merged_sorted = merged_sorted.OrderBy(o => o.id.ToString()).ToList();

            foreach (var i in merged_sorted)
            {
                var header = "uid." + i.id;
                foreach (var u in i.rawrows)
                {
                    outp.WriteLine(header + u);
                    //Console.WriteLine(header + u);
                }
            }

            outp.WriteLine("uid.length=" + (merged_uid.Max(c => c.id) + 1));
            outp.Close();
        }

        public void installmdata(string file)
        {
            StreamReader srdlc = new StreamReader(file);
            while (srdlc.Peek() > -1)
            {
                string line = srdlc.ReadLine();

                bool check = true;

                if (line.StartsWith("#"))
                    check = false;

                if (line.Contains("length"))
                    check = false;

                if (line.Contains("uid.max"))
                    check = false;

                if (check)
                {
                    string[] line2 = line.Split('.');
                    int id = int.Parse(line2[1]);

                    if (line2[0] == "category")
                    {
                        var curRawPv = ft_mdata.Where(c => c.id == id).FirstOrDefault();
                        if (curRawPv == null)
                        {
                            ft_mdata.Add(new a3d(id));
                            curRawPv = ft_mdata.Where(c => c.id == id).FirstOrDefault();
                            curRawPv.rawrows.Add(format_str(line2));
                        }
                        else
                        {
                            curRawPv.rawrows.Add(format_str(line2));
                        }

                        if (line2[2].Contains("value"))
                        {
                            var line3 = line2[2].Split('=');
                            curRawPv.value = line3[1];
                        }
                    }

                    if (line2[0] == "uid")
                    {
                        var curRawPv = ft_mdata_uid.Where(c => c.id == id).FirstOrDefault();
                        if (curRawPv == null)
                        {
                            ft_mdata_uid.Add(new a3d(id));
                            curRawPv = ft_mdata_uid.Where(c => c.id == id).FirstOrDefault();
                            if (!line.Contains("org_uid"))
                            {
                                curRawPv.rawrows.Add(format_str(line2));
                            }
                            
                        }
                        else
                        {
                        if (!line.Contains("org_uid"))
                            curRawPv.rawrows.Add(format_str(line2));
                        }
                        if (line2[2].Contains("value"))
                        {
                            var line3 = line2[2].Split('=');
                            curRawPv.value = line3[1];
                        }
                    }
                }
            }
            srdlc.Close();

            srdlc = new StreamReader(file);
            while (srdlc.Peek() > -1)
            {
                string line = srdlc.ReadLine();

                bool check = true;

                if (line.StartsWith("#"))
                    check = false;

                if (line.Contains("length"))
                    check = false;

                if (line.Contains("uid.max"))
                    check = false;

                if (check)
                {
                    string[] line2 = line.Split('.');
                    int id = int.Parse(line2[1]);
                    
                    if (line2[0] == "uid")
                    {
                        var curRawPv = ft_mdata_uid.Where(c => c.id == id).FirstOrDefault();
                        if (curRawPv == null)
                        {
                            if (line.Contains("org_uid"))
                            {
                                foreach (var i in ft_mdata_uid)
                                {
                                    if (i.id == id)
                                        i.id = int.Parse(line.Split('=')[1]);
                                }
                                Logs.WriteLine("mdata id " + int.Parse(line.Split('=')[1]), 1);
                            }

                        }
                        else
                        {
                            if (line.Contains("org_uid"))
                            {
                                foreach (var i in ft_mdata_uid)
                                {
                                    if (i.id == id)
                                        i.id = int.Parse(line.Split('=')[1]);
                                }
                                Logs.WriteLine("mdata id " + int.Parse(line.Split('=')[1]), 1);
                            }
                        }
                    }
                }
            }
            srdlc.Close();

            foreach (var i in ft_mdata)
            {
                i.id = a3d_dbs.Count();
                a3d_dbs.Add(i);
                Logs.WriteLine("merged from mdata " + i.id + "|" + String.Join(",", i.rawrows), 1);
            }

            foreach (var i in ft_mdata_uid)
            {
                a3d_dbs_uid.Add(i);
                Logs.WriteLine("merged from mdata_uid " + i.id + "|" + String.Join(",", i.rawrows), 1);
            }
        }

        public void loadmdata(string file)
        {
            StreamReader srdlc = new StreamReader(file);
            while (srdlc.Peek() > -1)
            {
                string line = srdlc.ReadLine();

                bool check = true;

                if (line.StartsWith("#"))
                    check = false;

                if (line.Contains("length"))
                    check = false;

                if (line.Contains("uid.max"))
                    check = false;

                if (check)
                {
                    string[] line2 = line.Split('.');
                    int id = int.Parse(line2[1]);

                    if (line2[0] == "category")
                    {
                        var curRawPv = a3d_dbs.Where(c => c.id == id).FirstOrDefault();
                        if (curRawPv == null)
                        {
                            a3d_dbs.Add(new a3d(id));
                            curRawPv = a3d_dbs.Where(c => c.id == id).FirstOrDefault();
                            curRawPv.rawrows.Add(format_str(line2));
                        }
                        else
                        {
                            curRawPv.rawrows.Add(format_str(line2));
                        }

                        if (line2[2].Contains("value"))
                        {
                            var line3 = line2[2].Split('=');
                            curRawPv.value = line3[1];
                        }
                    }

                    if (line2[0] == "org_uid")
                    {
                        var curRawPv = a3d_dbs_uid.Where(c => c.id == id).FirstOrDefault();
                        if (curRawPv == null)
                        {
                            a3d_dbs_uid.Add(new a3d(id));
                            curRawPv = a3d_dbs_uid.Where(c => c.id == id).FirstOrDefault();
                            //if (!line.Contains("org_uid"))
                            {
                                curRawPv.rawrows.Add(format_str(line2));
                            }
                        }
                        else
                        {
                            //if (!line.Contains("org_uid"))
                                curRawPv.rawrows.Add(format_str(line2));
                        }

                        if (line2[2].Contains("value"))
                        {
                            var line3 = line2[2].Split('=');
                            curRawPv.value = line3[1];
                        }
                    }
                }
            }
            srdlc.Close();
        }

        public void savemdata(string s)
        {
            int counter = 0;
            foreach (var i in a3d_dbs)
            {
                i.id = counter;
                counter++;
            }
            //int maxid = a3d_dbs_uid.Max(c => c.id);
            
            counter = 0;
            foreach (var i in a3d_dbs_uid)
            {
                i.org_id = i.id;
                i.id = counter;
                counter++;
            }


            StreamWriter outp = new StreamWriter(s, false, Encoding.ASCII);
            outp.NewLine = "\n";
            outp.WriteLine("#A3DA__________");
            outp.WriteLine("# date time was eliminated.");

            var merged_sorted = a3d_dbs.OrderBy(o => o.value).ToList();
            merged_sorted = merged_sorted.OrderBy(o => o.id.ToString()).ToList();

            //category
            foreach (var i in merged_sorted)
            {
                var header = "category." + i.id;
                foreach (var u in i.rawrows)
                {
                    outp.WriteLine(header + u);
                }
            }
            outp.WriteLine("category.length=" + (merged_sorted.Max(c => c.id) + 1));
            //a3d
            merged_sorted = a3d_dbs_uid.OrderBy(o => o.value.ToString()).ToList();
            merged_sorted = merged_sorted.OrderBy(o => o.id.ToString()).ToList();

            foreach (var i in merged_sorted)
            {
                var header = "uid." + i.id;
                foreach (var u in i.rawrows)
                {
                    outp.WriteLine(header + u);
                    if (u.Contains("category"))
                        outp.WriteLine(header + ".org_uid=" + i.org_id);
                }
            }

            outp.WriteLine("uid.length=" + (merged_sorted.Max(c => c.id) + 1));
            outp.Close();
        }
        public void Merge2()
        {
            StreamReader srdlc = new StreamReader("auth_3d_dlc.txt");
            StreamReader srac = new StreamReader("auth_3d_ft.txt");
            StreamWriter outp = new StreamWriter("auth_3d_output3.txt");

            while (srac.Peek() > -1)
            {
                string line = srac.ReadLine();
                string[] line2 = line.Split('.');

                int id = int.Parse(line2[1]);

                if (line2[0] == "category")
                {
                    var curRawPv = ac.Where(c => c.id == id).FirstOrDefault();
                    if (curRawPv == null)
                    {
                        ac.Add(new a3d(id));
                        curRawPv = ac.Where(c => c.id == id).FirstOrDefault();
                        curRawPv.rawrows.Add(format_str(line2));
                    }
                    else
                    {
                        curRawPv.rawrows.Add(format_str(line2));
                    }

                    if (line2[2].Contains("value"))
                    {
                        var line3 = line2[2].Split('=');
                        curRawPv.value = line3[1];
                    }
                }

                if (line2[0] == "uid")
                {
                    var curRawPv = ac_uid.Where(c => c.id == id).FirstOrDefault();
                    if (curRawPv == null)
                    {
                        ac_uid.Add(new a3d(id));
                        curRawPv = ac_uid.Where(c => c.id == id).FirstOrDefault();
                        curRawPv.rawrows.Add(format_str(line2));
                    }
                    else
                    {
                        curRawPv.rawrows.Add(format_str(line2));
                    }

                    if (line2[2].Contains("value"))
                    {
                        var line3 = line2[2].Split('=');
                        curRawPv.value = line3[1];
                    }
                }
            }

            while (srdlc.Peek() > -1)
            {
                string line = srdlc.ReadLine();
                string[] line2 = line.Split('.');

                int id = int.Parse(line2[1]);

                if (line2[0] == "category")
                {
                    var curRawPv = ft_mdata.Where(c => c.id == id).FirstOrDefault();
                    if (curRawPv == null)
                    {
                        ft_mdata.Add(new a3d(id));
                        curRawPv = ft_mdata.Where(c => c.id == id).FirstOrDefault();
                        curRawPv.rawrows.Add(format_str(line2));
                    }
                    else
                    {
                        curRawPv.rawrows.Add(format_str(line2));
                    }

                    if (line2[2].Contains("value"))
                    {
                        var line3 = line2[2].Split('=');
                        curRawPv.value = line3[1];
                    }
                }

                if (line2[0] == "uid")
                {
                    var curRawPv = ft_mdata_uid.Where(c => c.id == id).FirstOrDefault();
                    if (curRawPv == null)
                    {
                        ft_mdata_uid.Add(new a3d(id));
                        curRawPv = ft_mdata_uid.Where(c => c.id == id).FirstOrDefault();
                        if (!line.Contains("org_uid"))
                        {
                            curRawPv.rawrows.Add(format_str(line2));
                        }
                    }
                    else
                    {
                        if (!line.Contains("org_uid"))
                            curRawPv.rawrows.Add(format_str(line2));
                    }

                    if (line2[2].Contains("value"))
                    {
                        var line3 = line2[2].Split('=');
                        curRawPv.value = line3[1];
                    }

                    if (line2[2].Contains("org_uid"))
                    {
                        var line3 = line2[2].Split('=');
                        curRawPv.org_id = int.Parse(line3[1]);
                    }
                }
            }

            foreach (var o in ft_mdata_uid)
            {
                if (o.org_id != -2)
                {
                    Console.WriteLine("set " + o.id + "=" + o.org_id);
                    o.id = o.org_id;
                }
            }

            //MERGE CATEGORY
            foreach (var a in ac)
            {
                var check = merged.Where(c => c.value == a.value).FirstOrDefault();
                if (check == null)
                {
                    var check2 = merged.Where(c => c.id == a.id).FirstOrDefault();
                    if (check2 == null)
                    {
                        merged.Add(a);
                    }
                    else
                    {
                        int maxid = merged.Max(c => c.id) + 1;
                        a.id = maxid;
                        merged.Add(a);
                    }
                }
            }

            foreach (var a in ft_mdata)
            {
                var check = merged.Where(c => c.value == a.value).FirstOrDefault();
                if (check == null)
                {
                    var check2 = merged.Where(c => c.id == a.id).FirstOrDefault();
                    if (check2 == null)
                    {
                        merged.Add(a);
                    }
                    else
                    {
                        int maxid = merged.Max(c => c.id) + 1;
                        a.id = maxid;
                        merged.Add(a);
                    }
                }
            }

            //MERGE UIDS
            foreach (var a in ac_uid)
            {
                var check = merged_uid.Where(c => c.value == a.value).FirstOrDefault();
                if (check == null)
                {
                    var check2 = merged_uid.Where(c => c.id == a.id).FirstOrDefault();
                    if (check2 == null)
                    {
                        merged_uid.Add(a);
                    }
                    else
                    {
                        
                    }
                }

            }

            foreach (var a in ft_mdata_uid)
            {
                int max = merged_uid.Max(i => i.id);
                var check = merged_uid.Where(c => c.value == a.value).FirstOrDefault();
                if (check == null)
                {
                    var check2 = merged_uid.Where(c => c.id == a.id).FirstOrDefault();
                    if (check2 == null)
                    {
                        merged_uid.Add(a);
                    }
                    else
                    {
                        //Console.WriteLine("WARNING SET " + a.id + " TO " + (max + 1));
                        //a.id = max + 1;
                        merged_uid.Remove(check2);
                        merged_uid.Add(a);
                        //Console.WriteLine("OVERWRITE FROM DLC " + check2.value + a.value);
                    }
                }

            }

            outp.WriteLine("#A3DA__________");
            outp.WriteLine("# date time was eliminated.");

            var merged_sorted = merged.OrderBy(o => o.value).ToList();
            merged_sorted = merged_sorted.OrderBy(o => o.id.ToString()).ToList();

            foreach (var i in merged_sorted)
            {
                var header = "category." + i.id;
                foreach (var u in i.rawrows)
                {
                    outp.WriteLine(header + u);
                    Console.WriteLine(header + u);
                }
            }
            outp.WriteLine("category.length=" + (merged.Max(c => c.id) + 1));
            //a3d
            merged_sorted = merged_uid.OrderBy(o => o.value.ToString()).ToList();
            merged_sorted = merged_sorted.OrderBy(o => o.id.ToString()).ToList();

            foreach (var i in merged_sorted)
            {
                var header = "uid." + i.id;
                foreach (var u in i.rawrows)
                {
                    outp.WriteLine(header + u);
                    //Console.WriteLine(header + u);
                }
            }

            outp.WriteLine("uid.length=" + (merged_uid.Max(c => c.id) + 1));
            outp.WriteLine("uid.max=" + (merged_uid.Max(c => c.org_id) + 1));
            outp.Close();

        }

        public void Merge()
        {
            StreamReader sr = new StreamReader("auth_3d_ft.txt");
            StreamReader srdlc = new StreamReader("auth_3d_dlc.txt");
            StreamReader srac = new StreamReader("auth_3d_ac.txt");
            StreamWriter outp = new StreamWriter("auth_3d_output.txt");

            while (srac.Peek() > -1)
            {
                string line = srac.ReadLine();
                string[] line2 = line.Split('.');

                int id = int.Parse(line2[1]);

                if (line2[0] == "category")
                {
                    var curRawPv = ac.Where(c => c.id == id).FirstOrDefault();
                    if (curRawPv == null)
                    {
                        ac.Add(new a3d(id));
                        curRawPv = ac.Where(c => c.id == id).FirstOrDefault();
                        curRawPv.rawrows.Add(format_str(line2));
                    }
                    else
                    {
                        curRawPv.rawrows.Add(format_str(line2));
                    }

                    if (line2[2].Contains("value"))
                    {
                        var line3 = line2[2].Split('=');
                        curRawPv.value = line3[1];
                    }
                }

                if (line2[0] == "uid")
                {
                    var curRawPv = ac_uid.Where(c => c.id == id).FirstOrDefault();
                    if (curRawPv == null)
                    {
                        ac_uid.Add(new a3d(id));
                        curRawPv = ac_uid.Where(c => c.id == id).FirstOrDefault();
                        curRawPv.rawrows.Add(format_str(line2));
                    }
                    else
                    {
                        curRawPv.rawrows.Add(format_str(line2));
                    }

                    if (line2[2].Contains("value"))
                    {
                        var line3 = line2[2].Split('=');
                        curRawPv.value = line3[1];
                    }
                }
            }

            while (sr.Peek() > -1)
            {
                string line = sr.ReadLine();
                string[] line2 = line.Split('.');

                int id = int.Parse(line2[1]);

                if (line2[0] == "category")
                {
                    var curRawPv = ft.Where(c => c.id == id).FirstOrDefault();
                    if (curRawPv == null)
                    {
                        ft.Add(new a3d(id));
                        curRawPv = ft.Where(c => c.id == id).FirstOrDefault();
                        curRawPv.rawrows.Add(format_str(line2));
                    }
                    else
                    {
                        curRawPv.rawrows.Add(format_str(line2));
                    }

                    if (line2[2].Contains("value"))
                    {
                        var line3 = line2[2].Split('=');
                        curRawPv.value = line3[1];
                    }
                }

                if (line2[0] == "uid")
                {
                    var curRawPv = ft_uid.Where(c => c.id == id).FirstOrDefault();
                    if (curRawPv == null)
                    {
                        ft_uid.Add(new a3d(id));
                        curRawPv = ft_uid.Where(c => c.id == id).FirstOrDefault();
                    }
                    else
                    {
                        curRawPv.rawrows.Add(format_str(line2));
                    }

                    if (line2[2].Contains("value"))
                    {
                        var line3 = line2[2].Split('=');
                        curRawPv.value = line3[1];
                    }
                }
            }

            while (srdlc.Peek() > -1)
            {
                string line = srdlc.ReadLine();
                string[] line2 = line.Split('.');

                int id = int.Parse(line2[1]);

                if (line2[0] == "category")
                {
                    var curRawPv = ft_mdata.Where(c => c.id == id).FirstOrDefault();
                    if (curRawPv == null)
                    {
                        ft_mdata.Add(new a3d(id));
                        curRawPv = ft_mdata.Where(c => c.id == id).FirstOrDefault();
                        curRawPv.rawrows.Add(format_str(line2));
                    }
                    else
                    {
                        curRawPv.rawrows.Add(format_str(line2));
                    }


                    if (line2[2].Contains("value"))
                    {
                        var line3 = line2[2].Split('=');
                        curRawPv.value = line3[1];
                    }
                }

                if (line2[0] == "uid")
                {
                    var curRawPv = ft_mdata_uid.Where(c => c.id == id).FirstOrDefault();
                    if (curRawPv == null)
                    {
                        ft_mdata_uid.Add(new a3d(id));
                        curRawPv = ft_mdata_uid.Where(c => c.id == id).FirstOrDefault();
                        if (!line2[2].Contains("org_uid"))
                            curRawPv.rawrows.Add(format_str(line2));
                    }
                    else
                    {
                        if (!line2[2].Contains("org_uid"))
                            curRawPv.rawrows.Add(format_str(line2));
                    }
                    
                    if (line2[2].Contains("org_uid"))
                    {
                        var line3 = line2[2].Split('=');
                        curRawPv.org_id = int.Parse(line3[1]);
                    }

                    if (line2[2].Contains("value"))
                    {
                        var line3 = line2[2].Split('=');
                        curRawPv.value = line3[1];
                    }
                }
            }

            foreach (var o in ft_mdata_uid)
            {
                if (o.org_id != -2)
                {
                    o.id = o.org_id;
                }
            }

            //MERGE CATEGORY
            foreach (var a in ac)
            {
                var check = merged.Where(c => c.value == a.value).FirstOrDefault();
                if (check == null)
                {
                    var check2 = merged.Where(c => c.id == a.id).FirstOrDefault();
                    if (check2 == null)
                    {
                        merged.Add(a);
                    } else
                    {
                        int maxid = merged.Max(c => c.id) + 1;
                        a.id = maxid;
                        merged.Add(a);
                    }
                }
            }

            foreach (var a in ft)
            {
                var check = merged.Where(c => c.value == a.value).FirstOrDefault();
                if (check == null)
                {
                    var check2 = merged.Where(c => c.id == a.id).FirstOrDefault();
                    if (check2 == null)
                    {
                        merged.Add(a);
                    }
                    else
                    {
                        int maxid = merged.Max(c => c.id) + 1;
                        a.id = maxid;
                        merged.Add(a);
                    }
                }
            }

            foreach (var a in ft_mdata)
            {
                var check = merged.Where(c => c.value == a.value).FirstOrDefault();
                if (check == null)
                {
                    var check2 = merged.Where(c => c.id == a.id).FirstOrDefault();
                    if (check2 == null)
                    {
                        merged.Add(a);
                    }
                    else
                    {
                        int maxid = merged.Max(c => c.id) + 1;
                        a.id = maxid;
                        merged.Add(a);
                    }
                }
            }

            //MERGE UIDS
            foreach (var a in ac_uid)
            {
                    var check2 = merged_uid.Where(c => c.id == a.id).FirstOrDefault();
                    if (check2 == null)
                    {
                        merged_uid.Add(a);
                    }
                    else
                    {

                    }
                
            }

            foreach (var a in ft_mdata_uid)
            {
                    var check2 = merged_uid.Where(c => c.id == a.id).FirstOrDefault();
                    if (check2 == null)
                    {
                        merged_uid.Add(a);
                    }
                    else
                    {

                    }
                
            }

            foreach (var a in ft_uid)
            {

                    var check2 = merged_uid.Where(c => c.id == a.id).FirstOrDefault();
                    if (check2 == null)
                    {
                        merged_uid.Add(a);
                    }
                    else
                    {

                    }
                
            }
            outp.WriteLine("#A3DA__________");
            outp.WriteLine("# date time was eliminated.");

            foreach (var i in merged)
            {
                if (i.id != 0)
                {
                    i.tempid = i.id;
                    while (i.tempid <= 1000000)
                        i.tempid = i.tempid * 10;
                }
                
            }

            var merged_sorted = merged.OrderBy(o=> o.id.ToString()).ToList();

            foreach (var i in merged_sorted)
            {
                var header = "category." + i.id;
                foreach (var u in i.rawrows)
                {
                    outp.WriteLine(header + u);
                    Console.WriteLine(header + u);
                }
            }

            //output
            /*
            for (int i=1; i < 9; i++)
            {
                {
                    {
                        var obj = merged.Where(g => g.id == i).FirstOrDefault();
                        if (obj != null)
                        {
                            var header = "category." + obj.id;
                            foreach (var u in obj.rawrows)
                            {
                                outp.WriteLine(header + u);
                                Console.WriteLine(header + u);
                            }
                        }
                    }
                    {
                        int max = ((i + 1) * (int)(Math.Pow(10, 5)));
                        int curr = (i) * (int)(Math.Pow(10, 4));
                        while (curr < max)
                        {
                            var obj = merged.Where(g => g.id == (int)(curr)).FirstOrDefault();
                            bool yay = false;
                            int check = curr;

                            while (check >= 10)
                                check /= 10;
                            if (i == check)
                                yay = true;
                            else yay = false;

                            if ((yay) && (obj != null))
                            {
                                var header = "category." + obj.id;
                                foreach (var u in obj.rawrows)
                                {
                                    outp.WriteLine(header + u);
                                    Console.WriteLine(header + u);
                                }
                            }
                            curr++;
                        }
                    }
                }
            }
            */
            outp.WriteLine("category.length=" + (merged.Max(c => c.id) + 1));
            //a3d

            foreach (var i in merged_uid)
            {
                if (i.id != 0)
                {
                    i.tempid = i.id;
                    while (i.tempid <= 100000000)
                        i.tempid = i.tempid * 10;
                }

            }

            merged_sorted = merged_uid.OrderBy(o => o.id.ToString()).ToList();

            foreach (var i in merged_sorted)
            {
                var header = "uid." + i.id;
                foreach (var u in i.rawrows)
                {
                    outp.WriteLine(header + u);
                    //Console.WriteLine(header + u);
                }
            }

            /*
            foreach (var i in merged_uid)
            {
                var header = "uid." + i.id;
                foreach (var u in i.rawrows)
                {
                    outp.WriteLine(header + u);
                }
            }
            */
            outp.WriteLine("uid.length=" + (merged_uid.Max(c => c.id) + 1));
            outp.Close();
        }
    }
}
